/**
 * 合同协议维护 - 编辑页面
 * @author miaoyl
 * @date 2019/02/01
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            form: {
                GWDD007_NAME:null,
                GWDD008_NAME:null,
                GWAKL003:null,
                GWDD031_NAME:null
            },
            disabled:false,
            rules: {
                GWDD003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDD012: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                /*GWDD013: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],*/
                GWDD006: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDD005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDD031_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                crm_gwdc_gwdd003: []
            }
        },
        cols = [[
            {field: 'GWDD002',type: 'checkbox', title: '选择'}
            , {field: 'GWDD016', width: 80, title: '返利方式',
                filterDatas: app.datas_('crm_gwdc_gwdd016'),
                edit:'select'
            }
            , {field: 'GWDD017_NAME', width: 140, title: '返利名称',event:'openGwdd017'
                /*filterDatas: app.datas_('crm_gwdc_gwdd017'),
                edit:'select'*/
            }
            , {field: 'GWDD018', width: 140, title: '说明',edit:'text'}
            /*, {field: 'GWDD029', width: 140, title: '指标金额上限',edit:'text'}*/
            , {field: 'GWDD030', width: 140, title: '指标金额',edit:'text'}
            , {field: 'GWDD019', width: 80, title: '返利率%',edit:'text'}
            , {field: 'GWDD032_NAME', width: 80, title: '商品系列',event:'openGwdd032'}
            , {field: 'GWDD020', width: 140, title: '计算基准',
                filterDatas: app.datas_('crm_gwdc_gwdd020')
            }
            , {field: 'GWDD021', width: 100, title: '生效日期',edit:'date'}
            , {field: 'GWDD022', width: 100, title: '失效日期',edit:'date'}
            , {field: 'GWDD023', width: 180, title: '备注',edit:'text'}
            , {field: 'GWDD017', width: 180, title: '返利编号',hide:true}
            , {field: 'GWDD032', width: 180, title: '商品分类编号',hide:true}
        ]],
        callObj,
        listData = null,
        table,
        gwdc005Table,
        layOpenIndex
    ;

    var Gwdc005Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc005_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }]
                }, win);
            },

            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdc005Table = table.render({
                        elem: app.getByid(doc, 'gwdc005_edit_table'),
                        id: 'gwdc005_edit_table',
                        cols: cols,
                        height:'250',
                        page: false
                    });

                    //监听单元格编辑
                    table.on('edit(gwdc005_edit_table)', function (obj) {
                        if (obj.field == 'GWDD016') {
                            var GWDD016 = obj.value;
                            /*var GWDD017 = '';
                            if(GWDD016 == '1'){
                                GWDD017 = '1';
                            }else if(GWDD016 == '3'){
                                GWDD017 = '4';
                            }else if(GWDD016 == '2'){
                                GWDD017 = '2';
                            }
                            obj.updateOfEdit({
                                'GWDD016': GWDD016,
                                'GWDD017': GWDD017
                            });*/
                        }else if(obj.field == 'GWDD019'){
                            var GWDD019 = obj.value;
                            //var reg = /^(\d{1,2}(\.\d{1,2})?|100)$/;
                            if(GWDD019>=0 && GWDD019<=100){
                                    obj.updateOfEdit({
                                        'GWDD019': parseFloat(GWDD019).toFixed(2),
                                    });
                                }else {
                                obj.updateOfEdit({
                                        'GWDD019': '',
                                    });
                                app.showWarning("请输入0~100的数字！");
                                return ;
                            }
                        }else if(obj.field == 'GWDD021'){
                            if(obj.value < vueData.form.GWDD005){
                                obj.updateOfEdit({
                                    'GWDD021': vueData.form.GWDD005,
                                });
                                app.showWarning("单身生效日期不可小于单头生效日期！");
                                return ;
                            }else if(obj.value > obj.data.GWDD022){
                                obj.updateOfEdit({
                                    'GWDD021': vueData.form.GWDD005,
                                });
                                app.showWarning("生效日期不可大于失效日期！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWDD021': obj.value,
                                });
                            }
                        }else if(obj.field == 'GWDD022'){
                            if(obj.value > vueData.form.GWDD006){
                                obj.updateOfEdit({
                                    'GWDD022': vueData.form.GWDD006,
                                });
                                app.showWarning("单身失效日期不可大于单头失效日期！");
                                return ;
                            } else if(obj.value < obj.data.GWDD021){
                                obj.updateOfEdit({
                                    'GWDD022': vueData.form.GWDD006,
                                });
                                app.showWarning("生效日期不可大于失效日期！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWDD022': obj.value,
                                });
                            }
                        }else if(obj.field == 'GWDD030'){//指标金额下限
                            if(obj.data.GWDD016 != '3'){
                                obj.updateOfEdit({
                                    'GWDD030': '',
                                });
                                app.showWarning("只有返利方式为：3.年返时才可输入指标金额！");
                                return ;
                            }else {
                                var re = /^[0-9]+.?[0-9]*$/;//判断是否为数字
                                if((!re.test(obj.value)) || obj.value <= 0){
                                    obj.updateOfEdit({
                                        'GWDD030': '',
                                    });
                                    app.showWarning("指标金额下限为大于0的数字！");
                                    return ;
                                }/*else if(obj.data.GWDD029 != '' && obj.data.GWDD029 != '' && parseFloat(obj.value) > parseFloat(obj.data.GWDD029)){
                                    obj.updateOfEdit({
                                        'GWDD030': '',
                                    });
                                    app.showWarning("指标金额下限不能大于指标金额上限！");
                                    return ;
                                }*/
                            }
                        }/*else if(obj.field == 'GWDD029'){//指标金额上限
                            if(obj.data.GWDD016 != '3'){
                                obj.updateOfEdit({
                                    'GWDD029': '',
                                });
                                app.showWarning("只有返利方式为：4.年返时才可输入指标金额上限！");
                                return ;
                            }else {
                                var re = /^[0-9]+.?[0-9]*$/;//判断是否为数字
                                if((!re.test(obj.value)) || obj.data <= 0){
                                    obj.updateOfEdit({
                                        'GWDD029': '',
                                    });
                                    app.showWarning("指标金额上限为大于0的数字！");
                                    return ;
                                }else if(obj.data.GWDD030 != '' && obj.data.GWDD030 != '' && parseFloat(obj.value) < parseFloat(obj.data.GWDD030)){
                                    obj.updateOfEdit({
                                        'GWDD029': '',
                                    });
                                    app.showWarning("指标金额上限不能小于指标金额下限！");
                                    return ;
                                }
                            }
                        }*/
                    });

                    //监听单元格编辑
                    table.on('tool(gwdc005_edit_table)', function (obj) {
                        if (obj.event == 'openGwdd017') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwde003',
                                        columnList: [{label: '选项值', prop: 'GWDE003', width: '150'},
                                            {label: '选项名称', prop: 'GWDE004', width: '150'}
                                        ],
                                        searchParam:{
                                            GWDE002: '返利编号',
                                        }
                                    },
                                    cb: function (data) {
                                        obj.update({
                                            'GWDD017': data.GWDE003,
                                            'GWDD017_NAME': data.GWDE004
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择返利编号',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if (obj.event == 'openGwdd032') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwao001',
                                        columnList: [{label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                            {label: '说明', prop: 'GWAOL003', width: '150'},
                                            {label: '助记码', prop: 'GWAOL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.update({
                                            'GWDD032': data.GWAO001,
                                            'GWDD032_NAME': data.GWAOL003
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品系列',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    });
                });
            },

            tableReload: function () {
                gwdc005Table.reload({
                    data: listData
                });
            },

            addAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwdc005/add',
                    xdata: listData1,
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc005').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            editAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwdc005/edit',
                    xdata: {list1:listData1, GWDD001:vueData.form.GWDD001},
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc005').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            _call: function (tagData) {
                vueData.selectOptions.crm_gwdc_gwdd003 = [];
                if (tagData.isCheck) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    vueData.disabled = false;
                    app.ajax({
                        xurl: 'crm/gwdc005/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWDD001: result.data.data.GWDD001,
                                        GWDD004: result.data.data.GWDD004,
                                        GWDD005: result.data.data.GWDD005,
                                        GWDD006: result.data.data.GWDD006,
                                        GWDD007: result.data.data.GWDD007,
                                        GWDD008: result.data.data.GWDD008,
                                        GWDD007_NAME: result.data.data.GWDD007_NAME,
                                        GWDD008_NAME: result.data.data.GWDD008_NAME,
                                        GWDD013: 0,
                                        GWDD014: null,
                                        GWDD015: null
                                    },
                                    reset: true
                                });
                                listData = [];
                                self.tableReload();
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.disabled = true;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwdc005/getGwddData',
                        xdata: {GWDD001: callObj.data.GWDD001},
                        loading: true,
                        cb: function (result) {
                            listData = result.data.data;
                            self.tableReload();
                        }
                    });
                }
                app.ajax({
                    xurl: 'crm/gwdc005/getGwdeList',
                    xdata: {},
                    cb: function (result) {
                        vueData.selectOptions.crm_gwdc_gwdd003.pushArray(result.data.data);
                    }
                });
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['gwdd_t'],vueData.form);
                var vue = new Vue({
                    el: '#gwdc005_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        addData: function () {
                            listData = gwdc005Table.getDatas();
                            var count = 0;
                            if(listData.length > 0){
                                count = listData[listData.length-1].GWDD002;
                            }
                            listData.push({
                                GWDD002:count+1,
                                GWDD016: '',
                                GWDD017: '',
                                GWDD018: '',
                                GWDD029: '',
                                GWDD030: '',
                                GWDD019: '',
                                GWDD020: '1',
                                GWDD021: vueData.form.GWDD005,
                                GWDD022: vueData.form.GWDD006,
                                GWDD023: '',
                            });
                            self.tableReload();
                        },
                        cancle: function () {
                            self._closeTpl();
                        },
                        delData: function () {
                            var list = gwdc005Table.getDatas();
                            var checkStatus = table.checkStatus('gwdc005_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDD002: v.GWDD002
                                    });
                                });
                                listData = list;
                                self.tableReload();
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    if(vueData.form.GWDD005 > vueData.form.GWDD006){
                                        app.showError("生效日期不可大于失效日期！");
                                        return;
                                    }
                                    var listData_1 = gwdc005Table.getDatas();
                                    if(listData_1.length == 0){
                                        app.showError("返利设置不可为空！");
                                        return;
                                    }
                                    for (var i = 0; i < listData_1.length; i++) {
                                        var v = listData_1[i];
                                        if (v.GWDD016 == '' || v.GWDD016 == null || v.GWDD017 == '' || v.GWDD017 == null
                                           || v.GWDD019 == '' || v.GWDD019 == null
                                        ) {
                                            app.showWarning('返利方式、返利编号及返利率不允许为空!');
                                            return;
                                        }
                                        if(v.GWDD016 == '3'){
                                            if(v.GWDD030 === '' || v.GWDD030 == null){
                                                app.showWarning('当返利方式为：3.年返时，指标金额不允许为空!');
                                                return;
                                            }
                                        }
                                    }
                                    listData_1.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addAllData(listData_1);
                                    } else {
                                        self.editAllData(listData_1);
                                    }
                                }
                            });
                        },
                        openUser:function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '人员编号', prop: 'ID', width: '150'},
                                            {label: '人员名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDD007_NAME = data.NAME;
                                        vueData.form.GWDD007 = data.ID;
                                        vueData.form.GWDD008_NAME = data.ORG_NAME;
                                        vueData.form.GWDD008 = data.ORG_ID;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择申请人',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGWDD012:function(){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_gwak001_4',
                                        columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDD012 = data.GWAK001;
                                        vueData.form.GWAKL003 = data.GWAKL003;
                                        vueData.form.GWDD031 = data.GWAK001;
                                        vueData.form.GWDD031_NAME = data.GWAKL003;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGWDD031:function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_gwak001_4',
                                        columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDD031 = data.GWAK001;
                                        vueData.form.GWDD031_NAME = data.GWAKL003;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择经销商',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc005_edit');
                    }
                });

                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc005Edit();

})
;